package com.admin.service.impl;

import com.admin.entity.Theme;
import com.admin.mapper.ThemeMapper;
import com.admin.service.IApkService;
import com.admin.service.ITerminalService;
import com.admin.service.IThemeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@SuppressWarnings("all")
public class ThemeServiceImpl extends ServiceImpl<ThemeMapper, Theme> implements IThemeService {

    @Autowired
    private ThemeMapper themeMapper;
    @Autowired
    private ITerminalService iTerminalService;
    @Autowired
    private IApkService iApkService;

    /**
     * 查询所有主题
     *
     * @param page
     * @param limit
     * @param map
     * @return
     */
    @Override
    public IPage queryThemeAll(Integer page, Integer limit, Map map) {

        Page page1 = new Page(page, limit);

        IPage iPage = themeMapper.queryThemeAll(page1, map);

        return iPage;
    }

    /**
     * 查询所有管理主题和当前用户的主题
     *
     * @param page
     * @param limit
     * @param map
     * @return
     */
    @Override
    public IPage queryGuanLiThemeAll(Integer page, Integer limit, Map map) {

        Page page1 = new Page(page, limit);

        IPage iPage = themeMapper.queryGuanLiThemeAll(page1, map);

        return iPage;
    }

    /**
     * 添加主题
     *
     * @param theme
     * @return
     */
    @Override
    public int addTheme(Theme theme) {

        System.out.println("1");
        List<Theme> themes = themeMapper.selectList(null);

        System.out.println("2");
        for (Theme theme1 : themes) {

            if (theme.getThemeName().equals(theme1.getThemeName())) {
                return 0;
            }
        }

        System.out.println("theme\t"+theme);

        int insert = themeMapper.insert(theme);

        return insert;
    }

    /**
     * 修改主题
     *
     * @param theme
     * @return
     */
    @Override
    public int updateTheme(Theme theme) {

        int update = themeMapper.updateById(theme);

        return update;
    }

    @Override
    public Theme queryThemeStation(Integer themeId) {

        Theme theme = themeMapper.selectById(themeId);

        return theme;
    }
//
//    @Override
//    public int updateThemeById(Theme theme) {
//
//        int i = themeMapper.(theme);
//
//        return i;
//    }

    /**
     * 主题审核查询
     *
     * @param page
     * @param limit
     * @return
     */
    @Override
    public IPage selectPcuncheckedTheam(int page, int limit) {
        Page p = new Page(page, limit);
        return themeMapper.selectPcuncheckedTheam(p);
    }

    /**
     * 酒店/足浴欢迎界面审核
     */
    @Override
    public IPage selwelcomepageshenge(int page, int limit) {
        Page p = new Page(page, limit);
        IPage re = themeMapper.welcomeShenhe(p);
        return re;
    }

    /**
     * 查找欢迎界面未审核的数量并且吧其他几个审核的未审核数量融合在一个map里面
     *
     * @return
     */
    @Override
    public Map welcomeWeichenheCount() {
        Map map = new LinkedHashMap();
        QueryWrapper q = new QueryWrapper();
        q.isNull("theme_welcome_page_pcchecked");
        q.or();
        q.eq("theme_welcome_page_pcchecked", "");
        int count = themeMapper.selectCount(q);
        map.put("welcome",count);
        map.put("theme",themeWeishenheCount());
        map.put("terminal",iTerminalService.terminalWeishenheCount());
        map.put("apk",iApkService.apkWeishenheCount());
        return map;
    }

    /**
     * 查找主题未审核的数量
     *
     * @return
     */
    @Override
    public int themeWeishenheCount() {
        QueryWrapper q = new QueryWrapper();
        q.isNull("theme_audit_pc_audited");
        q.or();
        q.eq("theme_audit_pc_audited", "");
        int count = themeMapper.selectCount(q);
        return count;
    }
}
